import java.util.Arrays;

public class MyList implements IList{
    public int[] arr;
    public static final int LENGTH=10;
    int UseSize=0;
    public MyList(){
        this.arr=new int[LENGTH];
    }
    //添加元素
    @Override
    public void add(int data) {
        if(isFull()){
            chekCapacity();
        }
        this.arr[UseSize]=data;
        UseSize++;
    }
    @Override
    public void add(int pos, int data) {
        try {
            chekPos(pos);
        }catch (PosIllegality e){
            e.printStackTrace();
            return;
        }
        //元素往后挪,空出pos的位置
        for(int i=UseSize-1;i>=pos;i--){
            arr[i+1]=arr[i];
        }
        //把date值赋给pos位置
        arr[pos]=data;
        UseSize++;
    }

    public void chekCapacity(){
        arr= Arrays.copyOf(arr,arr.length*2);
    }
    public void chekPos(int pos)throws PosIllegality{
        if(pos<0||pos>UseSize){
            throw new PosIllegality("下标不合法"+pos);
        }
    }


    @Override
    public void remove(int toRemove) {
        int pos=indexOf(toRemove);
        if(pos==-1){
            System.out.println("没有这个数字");
        }
        for(int i=pos;i<UseSize-1;i++){
            arr[i]=arr[i+1];
        }
        UseSize--;
    }
    public void movePos(int pos)throws PosIllegality{
        if(pos<0||pos>=UseSize){
            throw new PosIllegality("下标不合法"+pos);
        }
    }
    //遍历顺序表
    @Override
    public void display() {
        for(int i=0;i<UseSize;i++){
            System.out.print(arr[i]+" ");
        }

    }
    //找是否包含某个元素
    @Override
    public boolean contains(int toFind) {
        if(UseSize==0){
            return false;
        }
        for(int i=0;i<UseSize;i++){
            if(arr[i]==toFind){
                return true;
            }
        }
        return false;
    }
    //找某个元素对应的位置
    @Override
    public int indexOf(int toFind) {
        for(int i=0;i<UseSize;i++){
            if(arr[i]==toFind){
                return i;
            }
        }
        return -1;
    }
    //获取pos位置的元素
    @Override
    public int get(int pos) {
        try {
            getandsetPos(pos);
        }catch (PosIllegality e){
            e.printStackTrace();
        }
        for (int i=0;i<UseSize;i++){
            return arr[pos];
        }
        return arr[pos];
    }
    public void getandsetPos(int pos)throws PosIllegality{
        if(pos<0||pos>=UseSize){
            throw new PosIllegality("下标不合法"+pos);
        }
    }

    @Override
    public void set(int pos, int value) {
        try {
            getandsetPos(pos);
        }catch (PosIllegality e){
            e.printStackTrace();
            return;
        }
        arr[pos]=value;
    }
    //获取顺序表长度
    @Override
    public int size() {
        return UseSize;
    }

    @Override
    public void clear() {

    }



    @Override
    public boolean isFull() {
      return UseSize==arr.length;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }
}
